package com.apes.project.jekunAuto.intf.crm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.crm.callCenter.model.CustomComplain;
import com.apes.crm.callCenter.repository.CustomComplainRepository;
import com.apes.crm.callCenter.service.CommonForCallCenterService;
import com.apes.crm.car.model.CarBase;
import com.apes.crm.customerCar.model.CustomerCar;
import com.apes.crm.customerCar.service.CustomerCarService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.query.SqlService;
import com.apes.scm.masterdata.contrast.model.ContrastPartyCode;
import com.apes.scm.masterdata.contrast.repository.ContrastPartyCodeRepository;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.enumeration.service.EnumerationService;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.service.DeptService;
import com.apes.scm.masterdata.party.model.Party;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRepository;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.party.service.PartyRoleService;
import com.apes.scm.rbac.repository.PostRepository;
import com.apes.scm.rbac.repository.UserRepository;
import com.apes.scm.rbac.service.UserService;
import com.apestech.framework.json.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.List;

@Service("intfCallCenterService")
public class callCenterService {

    @Autowired
    SqlService  sqlService;
    @Autowired
    CustomComplainRepository customComplainRepository;
    @Autowired
    EntityManager entityManager;
    @Autowired
    UserService userService;
    @Autowired
    UserRepository userRepository;
    @Autowired
    CommonForCallCenterService commonForCallCenterService;
    @Autowired
    ContrastPartyCodeRepository contrastPartyCodeRepository;
    @Autowired
    CustomerCarService customerCarService;
    @Autowired
    DeptService deptService;
    @Autowired
    com.apes.crm.car.repository.CarBaseRepository CarBaseRepository;
    @Autowired
    EnumerationService enumerationService;
    @Autowired
    PartyRoleService partyRoleService;
    @Autowired
    PostRepository postRepository;
    @Autowired
    PartyRoleRepository partyRoleRepository;
    @Autowired
    PartyRepository partyRepository;

    /***
     * 功能：同步客诉订单
     * @param request
     * @return
     */
    public String getcustomComplainForApp(SimpleRequest request){
        JSONObject json = request.getJO();
        com.apestech.framework.json.JSONObject param = new com.apestech.framework.json.JSONObject();
        try {
            param.put("TIMES", json.getString("TIMESTAMP"));
            param.put("nb_alldata", true);
            List list = sqlService.queryId("customComplain.queryCustomComplainForApp", param);
            return JSON.toJSONStringWithDateFormat(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /***
     * 功能：同步客诉订单
     * @param request
     * @return
     */
    /***
     * 功能： app创建客诉单接口
     * @param request
     * @return
     */
    public com.alibaba.fastjson.JSONObject appInsertCustomComplain(SimpleRequest request){
        JSONObject tempObj  = request.getJO(); // 获取对象

        CustomComplain customComplain = new CustomComplain();
        Dept dept= deptService.findOne(tempObj.getString("dept_id")); //获取门店对象
        if(dept == null) throw new  RuntimeException("未找到对应的门店信息");

        ContrastPartyCode contrastPartyCode= contrastPartyCodeRepository.findFirstByVipid(tempObj.getString("vip_id"));
        if(contrastPartyCode == null) throw new  RuntimeException("未找到对应的会员信息");

        //获取会员信息
        Party vipInfo = contrastPartyCode.getParty();
        try {
            customComplain.setVip(partyRoleService.findByPartyAndRole(vipInfo.getId(),"Customer"));
        }catch (Exception e){
            throw new  RuntimeException("未找到对应的会员信息");
        }

        //获取车辆信息
        CustomerCar customerCar = null;
        if(tempObj.getString("clnm") != null) {
            CarBase carInfo= CarBaseRepository.findFirstByOriginEquals(tempObj.getString("clnm"));
            if(carInfo != null) customerCar= customerCarService.findCarByVipInfoAndCarID(vipInfo,carInfo);
        }

        Enumeration typeEnumer = enumerationService.findById(tempObj.getString("customComplainType"));
        Enumeration sourceEnumer = enumerationService.findById(tempObj.getString("customComplainSource"));
        Enumeration categoryEnumer = enumerationService.findById(tempObj.getString("complainCategory"));
        Enumeration complainTypeEnumer = enumerationService.findById(tempObj.getString("complainType"));
        Enumeration complainLevelEnumer = enumerationService.findById(tempObj.getString("complainLevel"));
        Enumeration responsibilityEnumer = enumerationService.findById(tempObj.getString("responsibilityComplain"));


        customComplain.setDept(dept);
        customComplain.setCar(customerCar);
        customComplain.setCustomComplainType(typeEnumer);
        customComplain.setCustomComplainSource(sourceEnumer);
        customComplain.setComplainType(complainTypeEnumer);
        customComplain.setComplainCategory(categoryEnumer);
        customComplain.setComplainLevel(complainLevelEnumer);

        customComplain.setResponsibilityComplain(responsibilityEnumer);


        //服务单号 投诉内容 处罚方案
        customComplain.setServer(tempObj.getString("server"));
        customComplain.setComplainContent(tempObj.getString("complainContent"));
        customComplain.setPunishmentPlan(tempObj.getString("punishmentPlan"));

        //图片
        if(tempObj.get("infoPhotoUrl") != null){
            JSONArray array = tempObj.getJSONArray("infoPhotoUrl");
            for(int i = 0, len = array.size(); i < len; i++){
                JSONObject obj = array.getJSONObject(i);
                obj.put("name","外部图片");
                obj.put("type","image/jpeg");
            }
            customComplain.setImgs(tempObj.getJSONArray("infoPhotoUrl").toString());
        }

        //设置当前的状态
        customComplain.setState("create");

        //以公司为主体创建单据
        PartyRole company = partyRoleService.findOne(tempObj.getString("creator"), "Company");
        customComplain.setCreator(company.getId());
        customComplain.setCompany(company);
        customComplain = customComplainRepository.saveAndFlush(customComplain);

        JSONObject reJson = new JSONObject();
        reJson.put("id",customComplain.getId());
        return reJson;
    }


}
